package com.doge.service.wx.aftersale;

import com.doge.bean.bo.aftersale.AftersaleSubmitBO;
import com.doge.bean.pojo.aftersale.AfterSale;
import com.doge.bean.pojo.order.Order;
import com.doge.exception.Param402Exception;
import com.doge.bean.base.BaseData;
import com.doge.bean.pojo.aftersale.AfterSale;
import com.doge.bean.pojo.aftersale.AfterSaleExample;
import com.doge.bean.pojo.order.Order;
import com.doge.bean.pojo.order.OrderGoods;
import com.doge.bean.pojo.order.OrderGoodsExample;
import com.doge.bean.vo.wxaftersale.WxAftersaleDetail;
import com.doge.bean.vo.wxaftersale.WxAftersaleListVO;
import com.doge.mapper.AfterSaleMapper;
import com.doge.mapper.GoodsMapper;
import com.doge.mapper.OrderGoodsMapper;
import com.doge.mapper.OrderMapper;
import lombok.SneakyThrows;
import com.doge.util.auth.GetSubject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: project2-wechat-mall
 * @description:
 * @author: 305905917@qq.com
 * @create: 2021-12-03 22:45
 **/
@Service
public class WxAftersaleServiceImpl implements WxAftersaleService {

    @Autowired
    GoodsMapper goodsMapper;
    @Autowired
    AfterSaleMapper afterSaleMapper;
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    OrderGoodsMapper orderGoodsMapper;

    @SneakyThrows
    @Override
    public void submit(AftersaleSubmitBO aftersaleSubmitBO) {

        AfterSale afterSale = new AfterSale();

        // 获得当前日期时间的String
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        String yyyyMMdd = simpleDateFormat.format(date);

        int rs = (int) ((Math.random() * 9 + 1) * Math.pow(10, 5));
        String s = String.valueOf(rs);
        String concat = yyyyMMdd.concat(s);

        afterSale.setAftersaleSn(concat);
        afterSale.setOrderId(Integer.parseInt(aftersaleSubmitBO.getOrderId()));
        afterSale.setType((short) ((int) aftersaleSubmitBO.getType()));
        afterSale.setReason(aftersaleSubmitBO.getReason());
        afterSale.setAmount(new BigDecimal(aftersaleSubmitBO.getAmount()));
        afterSale.setPictures(aftersaleSubmitBO.getPictures());
        afterSale.setStatus((short) 1);
        afterSale.setAddTime(new Date());

        // 根据orderId查询order表得userId
        Order order = orderMapper.selectByPrimaryKey(Integer.parseInt(aftersaleSubmitBO.getOrderId()));

        // 更改order表中该订单的status：0变为1
        order.setAftersaleStatus((short)1);
        order.setRefundAmount(new BigDecimal(aftersaleSubmitBO.getAmount()));
        order.setRefundType(String.valueOf(aftersaleSubmitBO.getType()));
        //order.setRefundTime();

        int row1 = orderMapper.updateByPrimaryKeySelective(order);


        afterSale.setUserId(order.getUserId());

        int row2 = afterSaleMapper.insertSelective(afterSale);

        if (row1 != 1 && row2 != 1) {
            throw new Param402Exception("提交售后信息失败");
        }
    }

    @Override
    public BaseData list(Integer status, Integer page, Integer limit) {
        List<WxAftersaleListVO> wxAftersaleListVOList = new ArrayList<>();
        PageHelper.startPage(page, limit);
        if (GetSubject.getUser() != null) {
            Integer userId = GetSubject.getUserId();
            AfterSaleExample afterSaleExample = new AfterSaleExample();
            afterSaleExample.createCriteria().andUserIdEqualTo(userId).andStatusEqualTo(status).andDeletedEqualTo(false);
            List<AfterSale> afterSales = afterSaleMapper.selectByExample(afterSaleExample);
            for (AfterSale afterSale : afterSales) {
                OrderGoodsExample orderGoodsExample = new OrderGoodsExample();
                orderGoodsExample.createCriteria().andOrderIdEqualTo(afterSale.getOrderId()).andDeletedEqualTo(false);
                List<OrderGoods> orderGoods = orderGoodsMapper.selectByExample(orderGoodsExample);
                wxAftersaleListVOList.add(new WxAftersaleListVO(afterSale, orderGoods));
            }
        }
        PageInfo pageInfo = new PageInfo(wxAftersaleListVOList);
        int pages = pageInfo.getPages();
        long total = pageInfo.getTotal();
        BaseData baseData = new BaseData();
        baseData.setList(wxAftersaleListVOList);
        baseData.setPages(pages);
        baseData.setTotal(total);
        baseData.setLimit(limit);
        return baseData;
    }

    @Override
    public WxAftersaleDetail detail(Integer orderId) {
        WxAftersaleDetail wxAftersaleDetail = new WxAftersaleDetail();
        if (GetSubject.getUser() != null) {
//            Integer userId = GetSubject.getUserId();
            AfterSale afterSale = afterSaleMapper.selectsAftersaleByUserIdAndOrderId(orderId);
            Order order1 = orderMapper.selectByPrimaryKey(orderId);
            OrderGoodsExample orderGoodsExample = new OrderGoodsExample();
            orderGoodsExample.createCriteria().andOrderIdEqualTo(orderId).andDeletedEqualTo(false);
            List<OrderGoods> orderGoods = orderGoodsMapper.selectByExample(orderGoodsExample);
            wxAftersaleDetail.setAftersale(afterSale);
            wxAftersaleDetail.setOrder(order1);
            wxAftersaleDetail.setOrderGoods(orderGoods);
        }
        return wxAftersaleDetail;
    }
}
